package com.zzz.framework.model;

import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Accessors(chain = true)
@Data
public class AmisSource {
    private List<AmisOptions> options = new ArrayList<>();

    //设置默认值
    private String value;

    public static <T> AmisSource of(List<T> list, Function<T, String> laberGetter, Function<T, String> valueGetter) {
        AmisSource source = new AmisSource();
        if (CollectionUtils.isNotEmpty(list)) {
            source.setOptions(list.stream().map(t -> {
                AmisOptions options = new AmisOptions();
                options.setLabel(laberGetter.apply(t));
                options.setValue(valueGetter.apply(t));
                return options;
            }).collect(Collectors.toList()));

        }
        return source;
    }

    @Data
    static public class AmisOptions {
        private String label;
        private String value;
    }
}
